package com.wxhandle.cleandemo.application.dto.identity;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.wxhandle.cleandemo.core.domain.base.QueryFilter;
import com.wxhandle.cleandemo.core.domain.identity.SysRole;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import lombok.Data;

@Data
public class SysRoleParam implements QueryFilter<SysRole> {

    private String srhKey;

    private Long roleId;

    @Override
    public Specification<SysRole> getSpecification() {
        Specification<SysRole> specification = new Specification<SysRole>(){
            @Override
            public Predicate toPredicate(Root<SysRole> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate pc = criteriaBuilder.conjunction();
                List<Expression<Boolean>> es=pc.getExpressions();
                if(null != getSrhKey())
                    es.add(criteriaBuilder.or(criteriaBuilder.like(root.get("name"), getSrhKey()), criteriaBuilder.like(root.get("description"), getSrhKey())));
                if(roleId > 0)
                    es.add(criteriaBuilder.equal(root.join("SysUserRole").get("roleId"), getRoleId()));
                return pc;
            }

        };
        return specification;
    }

    @Override
    public Sort getSort() {
        return Sort.by(Sort.Direction.ASC, "id");
    }
}
